/// Setting up the ANES File for presidential coalitions  

use "/Users/joshuazingher/Desktop/Prospectus /anes_cdfdta/anes coalitions .dta"

sort year

rename VCF0704 presvote

rename VCF0703 voted


/// Generating total White respondants by year

by year: egen Whiteresp = count(race) if race==1 

/// Generating total number of White voters by year

by year: egen Whitevoted = count(voted) if race==1 & voted==3 

/// Generating total number of White non-voters by year

by year: egen WhiteNvoted = count(voted) if race==1 & voted==0 | race==1 &  voted==1 | race==1 & voted==2  

/// Generating total number of Non-White voters by year

by year: egen NWhitevoted = count(voted) if race~=1 & voted==3 

/// Generating total number of Non-White non-voters by year

by year: egen NWhiteNvoted = count(voted) if race~=1 & voted==0  | race~=1 & voted==1  | race~=1 & voted==2 

/// filling in the blanks

by year: egen whitevoted = max(Whitevoted)
by year: egen whiteNvoted = max(WhiteNvoted)
by year: egen Nwhitevoted = max(NWhitevoted)
by year: egen NwhiteNvoted = max(NWhiteNvoted)




/// Generating % race TO with Mosteller Correction

gen N = 1
gen a = [whitevoted * NwhiteNvoted] / [whiteNvoted * Nwhitevoted]


///Generating filler vars

gen aminone =(a-1)
gen AwhiteplusTO = (AWhite+aTO)
gen awhiteTO=(a*AWhite*aTO)

gen CwhiteTO = 1/ (2*(aminone)) * (N+(aminone*AwhiteplusTO) - sqrt((N+aminone*AwhiteplusTO)*(N+aminone*AwhiteplusTO) - (4*(aminone*awhiteTO))))

///Adjusting TO for the formula

replace CwhiteTO = CwhiteTO/(AWhite)



/// Moving to Partisanship - Generating White Republican Vote total by year

by year: egen whiterep = count(race) if presvote==2 & race==1

/// Generating White Non-Republican Vote by Year

by year: egen whiteNrep = count(race) if presvote==1 & race==1

// Generating non-White Republican Vote total by year

by year: egen Nwhiterep = count(race) if presvote==2 & race~=1

/// Generating Non-White Non-Republican vote total by year

by year: egen NwhiteNrep = count(race) if presvote==1 & race~=1

/// filling in the blanks

by year: egen WhiteRep = max(whiterep)
by year: egen WhiteNRep = max(whiteNrep)
by year: egen NWhiteRep = max(Nwhiterep)
by year: egen NWhiteNRep = max(NwhiteNrep)

/// Generation % race Repocrat with Mosteller Correction

gen A = (WhiteRep*NWhiteNRep)/(WhiteNRep*NWhiteRep)
gen Aminone =(A-1)
gen AwhiteplusRep = ((AWhite)+(ARep))
gen AwhiteRep=(A*(AWhite)*(ARep))
gen CwhiteRep = 1/(2*(Aminone)) * (N+(Aminone*AwhiteplusRep) - sqrt((N+Aminone*AwhiteplusRep)*(N+Aminone*AwhiteplusRep) - (4*(Aminone*AwhiteRep))))

replace CwhiteRep = CwhiteRep/(AWhite)


/// Generating number of ANES respondents by year

by year: egen totalresp = count (VCF0006a)


/// Coalition Contribution

gen whitecoalcontrib = (CwhiteTO* CwhiteRep *AWhite)/(aTO*ARep)



